package cn.tedu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;

public class ThreadSum {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService es = new ThreadPoolExecutor(5, 5, 100, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.AbortPolicy());//创建核心线程数为5

        List<Future<Integer>> l = new ArrayList<>();
        int x;
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        int n = x / 5;
        for (int i = 1; i <= 4; i++) {
            MyCall m = new MyCall((i - 1) * n + 1, i * n);
            Future<Integer> f = es.submit(m);
            l.add(f);
        }
        MyCall m = new MyCall(4 * n + 1, x);
        Future<Integer> f = es.submit(m);
        l.add(f);
        int sum = 0;
        for (Future<Integer> k: l) {
            sum += k.get();
        }
        System.out.println("总结果为:" + sum);
        es.shutdown();
    }
}
class MyCall implements Callable<Integer> {

    private int begin, end;
    public MyCall(int begin, int end){
        this.begin = begin;
        this.end = end;
    }
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = begin; i <= end; i++) {
            sum += i;
        }
        System.out.println(Thread.currentThread().getName() + " " + begin + "到" + end);
        return sum;
    }
}
